<?php

class Admin_Admin_Controller extends Base_Controller {

  

  public function action_admin()
  { 
    $k = Input::get('k');
    $data['users_admin.username'] = array('like','%'.$k.'%');
    $data['users_admin.permission'] = array('like','%'.$k.'%');
    $username = Session::get('username');
    $users = DB::table('users_admin')
          ->where(function($query) use($data) {
          foreach($data as $column => $val)
          {
              $query->or_where($column, $val[0], $val[1]);
          }
          })
          ->where('dele','=',1)->paginate(10);
    return View::make('admin/administrator',array('users'=>$users,'k'=>$k));
 

  }


  public function action_addoper()
  {
    $ad = Input::get('ad');
    return View::make('admin/alertaddoper',array('ad'=>$ad));
  }


  public function action_doaddoper()
  {
    $ad = Input::get('ad');
    $username = Input::get('username');
    $password = Input::get('password');
    $data = date('Y-m-d H:i:s',time());
    $permission = "运维管理员";
    $explanation = "no description";
    $code = 1;
    $str1 = DB::table('users_admin')->where('username','=',$username)->first();
    if($str1){
        return json_encode(array('code'=>'40','msg'=>'账号已存在'));
    }else{
        $str = DB::table('users_admin')->insert(array('username'=>$username,'password'=>$password,'permission'=>$permission,'explanation'=>$explanation,'addtime'=>$data,'code'=>$code));

        if($str){
            return json_encode(array('code'=>'20','msg'=>'添加成功'));
        }else{
            return json_encode(array('code'=>'40','msg'=>'添加失败'));
        }
    }
  }



  public function action_dele()
  {
    $id = Input::get('id');
    $dele = 0;
    $str = DB::table('users_admin')->where('id','=',$id)->update(array('dele'=>$dele));
    if($str){
        $data = array(
            'code'=>'200',
            'msg'=>'删除成功'
            );
    }else{
        $data = array(
            'code'=>'400',
            'msg'=>'删除失败'
            );
    }
    return json_encode($data);
    }


  public function action_change()
  {
    $id = Input::get('id');
    $str = DB::table('users_admin')->where('id','=',$id)->first();
    return View::make('admin/alertadminchange',array('str'=>$str));
  }



  public function action_dochange()
  {


    $id = Input::get('id');
    $username = Input::get('username');
    $password = Input::get('password');
    $str = DB::table('users_admin')->where('id','=',$id)->update(array('username'=>$username,'password'=>$password));
    if($str){
          return json_encode(array('code'=>'20','msg'=>'修改成功'));
      }else{
          return json_encode(array('code'=>'40','msg'=>'修改失败'));
      }

  }


}